package com.feeyin.radar.crm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.service.MyRecycleBinService;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;

public class MyRecycleBinServiceImpl
  implements MyRecycleBinService
{
  private CustomerDAO customerDAO;
  private SaleStageSetDAO saleStageSetDAO;
  private CustomerShareDAO customerShareDAO;
  private SaleRecordDAO saleRecordDAO;
  private UserDAO userDAO;

  public UserDAO getUserDAO() {
	return userDAO;
}

public void setUserDAO(UserDAO userDAO) {
	this.userDAO = userDAO;
}

public CustomerDAO getCustomerDAO()
  {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public SaleRecordDAO getSaleRecordDAO() {
    return this.saleRecordDAO;
  }

  public void setSaleRecordDAO(SaleRecordDAO saleRecordDAO) {
    this.saleRecordDAO = saleRecordDAO;
  }

  public List getPageDiscardCustomers(String customerName, Integer currentUserId, Page page)
  {
    if (currentUserId == null) return null;

    Customer customer = new Customer();
    customer.setCustomerCategory("5");
    customer.setSalePersonId(currentUserId);
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }

    List list = new ArrayList();
    list = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (list == null) return null;
    for (int i = 0; i < list.size(); i++) {
      Customer newCustomer = new Customer();
      newCustomer = (Customer)list.get(i);
      
      Users user = this.userDAO.getUser(newCustomer.getSalePersonId());
      if (user != null) {
    	  newCustomer.setSalePerson(user.getUserName());
      }         

      newCustomer.setLastLink(this.saleRecordDAO.getSaleRecordLastLinkByCustomerId(newCustomer.getId()));

      if ((newCustomer != null) && (newCustomer.getSaleStageId() != null)) {
        SaleStageSet saleStage = new SaleStageSet();
        saleStage = this.saleStageSetDAO.getSaleStageByStageCode(newCustomer.getSaleStageId().toString());
        if (saleStage != null) {
          newCustomer.setSaleStage(saleStage.getStageName());
        }
      }
    }
    return list;
  }

  public Integer getPageDiscardCustomersCount(String customerName, Integer currentUserId)
  {
    if (currentUserId == null) return null;

    Customer customer = new Customer();
    customer.setCustomerCategory("5");
    customer.setSalePersonId(currentUserId);
    if (!StringUtils.isEmpty(customerName)) {
      customer.setCustomerName(customerName);
    }
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public boolean againPickUpCustomer(String[] ids, Integer currentUserId) {
    if ((ids == null) || (ids.length <= 0)) return false;
    if (currentUserId == null) return false;

    Customer customer = new Customer();
    customer.setCustomerCategory("1");

    for (int i = 0; i < ids.length; i++) {
      customer.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      this.customerDAO.updateCustomer(customer);

      this.customerShareDAO.insertCustomerShare(Integer.valueOf(Integer.parseInt(ids[i])), currentUserId, "1");
    }
    return true;
  }

  public boolean turnToOpenCustomer(String[] ids) {
    if ((ids == null) || (ids.length <= 0)) return false;

    Customer customer = new Customer();
    customer.setCustomerCategory("0");
    customer.setSalePersonId(Integer.valueOf(0));
    customer.setSaleStageId(Integer.valueOf(0));
    for (int i = 0; i < ids.length; i++) {
      customer.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      this.customerDAO.updateCustomer(customer);
    }
    return true;
  }

  public boolean deleteCustomer(String[] ids, Integer currentUserId) {
    if (currentUserId == null) return false;
    if ((ids == null) || (ids.length <= 0)) return false;

    Customer customer = new Customer();
    customer.setCustomerCategory("6");
    for (int i = 0; i < ids.length; i++) {
      customer.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      customer.setDiscardPersonId(currentUserId);
      this.customerDAO.updateCustomer(customer);
    }
    return true;
  }
}